﻿//返回指定的对象
function $(element) { return element = document.getElementById(element); }

// 格式化日期
Date.prototype.format = function(formatter) {
    if (!formatter || formatter == "") {
        formatter = "yyyy-MM-dd";
    }
    var year = this.getFullYear().toString();
    var month = (this.getMonth() + 1).toString();
    var day = this.getDate().toString();
    var yearMarker = formatter.replace(/[^y|Y]/g, '');
    if (yearMarker.length == 2) {
        year = year.substring(2, 4);
    }
    var monthMarker = formatter.replace(/[^m|M]/g, '');
    if (monthMarker.length > 1) {
        if (month.length == 1) {
            month = "0" + month;
        }
    }
    var dayMarker = formatter.replace(/[^d]/g, '');
    if (dayMarker.length > 1) {
        if (day.length == 1) {
            day = "0" + day;
        }
    }
    return formatter.replace(yearMarker, year).replace(monthMarker, month).replace(dayMarker, day);
}
// 获取首月第一天
function GetFirstDayOfMonth() {
    var date = new Date();
    var year = date.getFullYear();
    var month = (date.getMonth() + 1).toString();
        if (month.length == 1) {
        month = "0" + month;
    }
    return year + "-" + month + "-01";
}


// 累计列的合计行数值
function UpdateSum(cellName, isPercent) {
    var intCount = appGridE.GetRowCount();
    var intSum = 0.00;
    var i;
    var strValue;
    var intAddRowCount = 0;
    for (i = 0; i < intCount; i++) {
        strValue = appGridE.GetValue(i, cellName)
        if (strValue != null && !isNaN(strValue)) {
            intSum = Float.Add(intSum, strValue);
            intAddRowCount++;
        }
    }

    // 如果统计的是百分比，则需要除以有效累加的行数获取百分比
    if (isPercent && intAddRowCount != 0) {
        intSum = Float.Div(intSum, intAddRowCount);
    }
    return intSum;
}

// 格式化Grid列表的值
function formatGrid2Num(intRowCount, aryColumn, strFormat) {
    var intRowIndex, intColIndex;
    var intColCount = aryColumn.length;
    var tdTemp;
    for (intRowIndex = 0; intRowIndex < intRowCount; intRowIndex++) {
        for (intColIndex = 0; intColIndex < intColCount; intColIndex++) {
            tdTemp = appGridE.GetTd(intRowIndex, aryColumn[intColIndex]).firstChild;
            tdTemp.innerHTML = FormatNumber(tdTemp.innerHTML, strFormat);
        }
    }
}

// 格式化数值
// 默认格式#,##0
/* 引用该方法的页面需要插入以下XML代码
<xml id="NumberFormatXml">
	<number>
		<val>0</val>
		<format>#,##0.00</format>
	</number>
</xml>
<XML ID="NumberFormatXslt">
	<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
		<xsl:output method="text"/>
		<xsl:template match='number'>
			<xsl:value-of select='format-number(val,format)' />
		</xsl:template>
	</xsl:stylesheet>
</XML>
*/
function FormatNumber(Val, format) {
    if (isNaN(Val) || Val == "　" || Val == "" || Val == null) {
        return "-";
    } 
    // 数字则格式化
    // 默认格式#,##0
    if (!format || format == "") {
        format = "#,##0";
    }   
    NumberFormatXml.selectSingleNode("/number/val").text = Val;
    NumberFormatXml.selectSingleNode("/number/format").text = format;
    return NumberFormatXml.transformNode(NumberFormatXslt);
}	